<?php


namespace app\api\controller;
//https://cosimo.qswh.net.cn/api/Time/cancelBind
//http://cosmo.com/api/Time/prompt
use app\api\model\Bind;
use app\api\model\Member;
use app\api\model\Msg;
use think\Request;

class Time extends BaseApi {

    protected $bindItem;

    public function __construct()
    {
        $bind = new Bind();
        $this->bindItem = $bind->field('binds,residue')->find();
    }

    //生成通知消息
    public function prompt(){
//        $member = new Member();
//        $time = $this->bindItem->binds - $this->bindItem->residue;
//        //查询出全部的事业合伙人
//        $business = $member->where('identity',1)->select();
//        if(!$business->isEmpty()){
//            foreach ($business as $bu){
//                $businessOffline = $member->where('agentid',$bu->id)->select();
//                foreach ($businessOffline as $offline){
//                    if($offline->bindtime + $time * 24 * 60 * 60 <= time()){//提前一天提示
//                        $msg = new  Msg();
//                        $msgItem = $msg->where('offid',$offline->id)->order('id desc')->find();
//                        if(empty($msgItem) || time() > ($msgItem->createtime + $this->bindItem->binds * 24 * 60 * 60)){
//                            $msg->offid = $offline->id;
//                            $msg->openid = $bu->openid;
//                            $msg->bindtime = $offline->bindtime;
//                            $msg->createtime = time();
//                            $msg->save();
//                        }
//                    }
//                }
//            }
//        }
//        $sales = $member->where('identity', 2)->select();
        //事业合伙人过期下线
        $this->dealPrompt(1);
        //销售合伙人过期下线
        $this->dealPrompt(2);




    }

    //取消绑定
    public function cancelBind(){
        $period = $this->bindItem->binds;
        $member = new Member();
        $memberList = $member->where('identity',2)->whereOr('identity',3)->select();
        if(!$memberList->isEmpty()){
           foreach ($memberList as $list){
               if($list->bindtime + $period * 24 * 60 * 60  <time()){
                   $list->bindtime = 0;
                   $list->agentid = 0;
                   $list->save();
               }
           }
        }
    }


    protected function dealPrompt($identity){
        $member = new Member();
        $time = $this->bindItem->binds - $this->bindItem->residue;
        //查询出全部的事业合伙人
        $business = $member->where('identity',$identity)->select();
        if(!$business->isEmpty()){
            foreach ($business as $bu){
                $businessOffline = $member->where('agentid',$bu->id)->select();
                foreach ($businessOffline as $offline){
                    if($offline->bindtime + $time * 24 * 60 * 60 <= time()){//提前一天提示
                        $msg = new  Msg();
                        $msgItem = $msg->where('offid',$offline->id)->order('id desc')->find();
                        if(empty($msgItem) || time() > ($msgItem->createtime + $this->bindItem->binds * 24 * 60 * 60)){
                            $msg->offid = $offline->id;
                            $msg->openid = $bu->openid;
                            $msg->bindtime = $offline->bindtime;
                            $msg->createtime = time();
                            $msg->save();
                        }
                    }
                }
            }
        }
    }

}